package com.bert.controller;

import com.bert.entity.LogInfoEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.core.async.AsyncLoggerContextSelector;
import org.apache.logging.log4j.util.PropertiesUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/check")
public class Check2Controller {

    @GetMapping("/log4j2Async")
    public LogInfoEntity checkLog4j2Async(){
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        Boolean async = AsyncLoggerContextSelector.isSelected();
        String contextSelector = PropertiesUtil.getProperties().getStringProperty("log4j2.ContextSelector");
        String asyncLoggerRingBufferSize = PropertiesUtil.getProperties().getStringProperty("log4j2.asyncLoggerRingBufferSize");
        String asyncLoggerConfigRingBufferSize = PropertiesUtil.getProperties().getStringProperty("log4j2.asyncLoggerConfigRingBufferSize");
        String asyncQueueFullPolicy = PropertiesUtil.getProperties().getStringProperty("log4j2.asyncQueueFullPolicy");
        String discardThreshold = PropertiesUtil.getProperties().getStringProperty("log4j2.discardThreshold");

        return new LogInfoEntity(async, contextSelector, asyncLoggerRingBufferSize, asyncLoggerConfigRingBufferSize, asyncQueueFullPolicy, discardThreshold);
    }

    @GetMapping("/zero")
    public String zero() throws InterruptedException {
        Date date = new Date();
        return ZoneId.systemDefault() + " || " + date + " || " + LocalDateTime.now();
    }

}
